// pages/user/myOrderList/myOrderList.js
var app=getApp();
var _PAGESIZE = 10;//默认页量
var _PAGE = 1;//默认页码
var cancelsOrder = false;//取消
var sureOrder = false;//确认收货
var load= true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tables:0,//选项卡
    myorderlist:[],//订单列表
    hasMore: false, //数据是否加载完成
    loading: false,

    status: '',//审核态状态 1待审核2审核通过3审核失败
    load_show: false,//整体加一个开关 加载
    is_realname: '',//是否实名
    realnameShow:false,//弹窗显示
    order_id:'',
    authInfo: { 'status_name': '待认证', status: '' }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      myorderlist: [],
      tables: options.tables
    })
    that.getRealnameInfo();
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    _PAGE = 1;
    // that.getAuthInfo();
    var tables = that.data.tables;
    var new_type = '', pin_show = '';
    if (tables == 0) {
      new_type = '';
    } else if (tables == 1) {
      new_type = 1;
    } else if (tables == 2) {
      new_type = 2;
    } else if (tables == 3) {
      new_type = 3;
    } else if (tables == 4) {
      new_type = 4;
    } else if (tables == 5) {
      pin_show = 1;
      new_type = 4;
    } else if (tables == 6) {
      new_type = 6;
    }
    that.getMyRentalLists(new_type, pin_show);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    var tables = that.data.tables;
    var new_type = '', pin_show = '';
    if (tables == 0) {
      new_type = '';
    } else if (tables == 1) {
      new_type = 1;
    } else if (tables == 2) {
      new_type = 2;
    } else if (tables == 3) {
      new_type = 3;
    } else if (tables == 4) {
      new_type = 4;
    } else if (tables == 5) {
      pin_show = 1;
      new_type = 4;
    } else if (tables == 6) {
      new_type= 6;
    }
      _PAGE++;
    that.getMyRentalLists( new_type, pin_show);
    
  },
  // 切换选项
  changeTable: function (e) {
    var that = this;
    var tables = e.currentTarget.dataset.tables;
    _PAGE = 1;
      that.setData({
        tables: e.target.dataset.tables,
        myorderlist: [],
        loading: false,
        load_show: false
      })
    load= true;
      var new_type='',pin_show='';
      if (tables == 0) {
        new_type = '';
      } else if (tables == 1) {
        new_type = 1;
      } else if (tables == 2) {
        new_type = 2;
      } else if (tables == 3) {
        new_type = 3;
      } else if (tables == 4) {
        new_type = 4;
      } else if (tables == 5) {
        pin_show = 1;
        new_type = 4;
      } else if (tables == 6) {
        new_type = 6;
      }
    that.getMyRentalLists( new_type, pin_show);
  },
  //取消订单
  cancelOrder(e){
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '是否取消该订单',
      confirmColor: '#c80000',
      success: function (res) {
        if (res.confirm) {
          if(cancelsOrder){
              return false
          }
          cancelsOrder=true;
          app.ajax({
            url: 'User/Order/cancelOrder',
            method: 'POST',
            data:{
              order_id: order_id
            },
            success: function(res){
              app.showToast(res.data.message);
              if(res.data.code ==1000){
                var timer = setTimeout(function(){
                  var tables = that.data.tables;
                  var new_type = '', pin_show = '';
                  if (tables == 0) {
                    new_type = '';
                  } else if (tables == 1) {
                    new_type = 1;
                  } else if (tables == 2) {
                    new_type = 2;
                  } else if (tables == 3) {
                    new_type = 3;
                  } else if (tables == 4) {
                    new_type = 4;
                  } else if (tables == 5) {
                    pin_show = 1;
                    new_type = 4;
                  } else if (tables == 6) {
                    new_type = 6;
                  }
                  that.getMyRentalLists(new_type, pin_show);                 
                },1000)
              }
              cancelsOrder=false;
            }
          })
                      
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //跳转订单详情
  jumpDetail(e){
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
      wx.navigateTo({
        url: '../myOrderDetail/myOrderDetail?order_id=' + order_id,
      })
  },
  // 提醒发货
  remindOrder: function(){
    app.showToast("该功能正在开发中，敬请期待");
  },
  // 查看物流
  lookLogistics: function(e){
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../logistics/logistics?order_id=' + order_id,
    })
  },
  // 确认收货
  confirmReceipt: function(e){
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '是否确认收货',
      confirmColor: '#c80000',
      success: function (res) {
        //  if (sureOrder){
        //  return false
        //  }
        sureOrder=true;
        if (res.confirm) {
          app.ajax({
            url: 'User/Order/collectGoods',
            data: {
              order_id: order_id
            },
            success: function (res) {
              if (res.data.code == 1000) {
                app.showToast(res.data.message + ',请前去预约安装');
                var timer = setTimeout(function () {
                  // var tables = that.data.tables;
                  var tables = 3;
                  that.setData({
                    tables: tables
                  })
                  var new_type = '', pin_show = '';
                  if (tables == 0) {
                    new_type = '';
                  } else if (tables == 1) {
                    new_type = 1;
                  } else if (tables == 2) {
                    new_type = 2;
                  } else if (tables == 3) {
                    new_type = 3;
                  } else if (tables == 4) {
                    new_type = 4;
                  } else if (tables == 5) {
                    pin_show = 1;
                    new_type = 4;
                  } else if (tables == 6) {
                    new_type = 6;
                  }
                  that.getMyRentalLists(new_type, pin_show);
                }, 1000)
              } else {
                app.showToast(res.data.message);
              }
              sureOrder=false;
            }
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  //预约安装
  appointment(e) {
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    var status = that.data.authInfo.status;
    if (status == 2) {
      that.setData({
        realnameShow: false
      });
      wx.navigateTo({
        url: '../appointment/appointment?order_id=' + order_id,
      })
    } else {
      that.setData({
        realnameShow: true,
        order_id: order_id
      })
    }
  },
  // 关闭弹窗
  cancelModal: function () {
    var that = this;
    that.setData({
      realnameShow: false,
      order_id: ''
    })
  },

  // 继续预约
  goAppointment: function(){
    var that = this;
    var order_id = that.data.order_id;
    wx.navigateTo({
      url: '../appointment/appointment?order_id=' + order_id,
    })
  },
    //去评论
  evaluate(e){
    var that=this;
    var order_id = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../goEvaluate/goEvaluate?order_id=' + order_id,
    })
  },
  //去支付
  goPayfor(e){
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
      wx.navigateTo({
        url: '../../shopping/payment/payment?order_id='+order_id,
      })
  },
  //获取我的订单列表
  getMyRentalLists(status_type,pin_show){
    var that = this;
    app.ajax({
      url: 'User/Order/getOrderList',
      method: "POST",
      load: load,
      msg: '加载中...',
      data: { page: _PAGE, pageSize: _PAGESIZE, order_status: status_type, is_show: pin_show},
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1000) {
          if (_PAGE == 1) {
            that.setData({
              myorderlist: res.data.data,
              empty: false
            });
          } else {
            that.setData({
              myorderlist: that.data.myorderlist.concat(res.data.data),
              empty: false
            });
          }
          var hasMore = true;
          if (res.data.data.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            hasMore: hasMore,
            loading: false
          });
          load= false;
        }  else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }
        }
        that.setData({
          load_show: true
        })
        
      }
    })
  },
  /*//获取是否实名
  getAuthInfo() {
    var that = this;
    var order_id = that.data.order_id;
    app.ajax({
      url: 'User/User/getAuthInfo',
      method: "POST",
      data: {},
      success: function (res) {
        wx.hideLoading();
        if (res.data.code == 1000) {
          that.setData({
            status: res.data.data.status
          })
        } else if (res.data.code == -1003) {
          that.setData({
            status: ''
          })
        }
      }

    })
  },*/
  // 售后记录详情
  goDetail: function (e) {
    //console.log(e.currentTarget.dataset.detail);
    wx.navigateTo({
      url: '../afterSaleDetail/afterSaleDetail?detail=' + e.currentTarget.dataset.work_order_id,
    })
  },
  //图片加载错误
  Order_error(e) {
    var that = this, myorderlist = that.data.myorderlist, nowIndex = e.currentTarget.dataset.nowindex;
    myorderlist[nowIndex].main_pic = '../../../images/none.png'
    that.setData({
      myorderlist: myorderlist
    })
  },

  // 签署电子合同
  signContract(e){
    wx.navigateTo({
      url: '../econtract/econtract?contract_id=' + e.currentTarget.dataset.contract_id,
    })
  },
  //跳转我的认证
  goRealname() {
    var that = this;
    if (that.data.authInfo.status == -2) {
      wx.navigateTo({ //../../shopping/uploadDocuments/uploadDocuments
        url: '../../shopping/uploadDocuments/uploadDocuments',
      })
    } else if (that.data.authInfo.status == -3) {
      wx.navigateTo({
        url: '../../shopping/faceRecognition/faceRecognition',
      })
    } else if (that.data.authInfo.status == 2 || that.data.authInfo.status == 1) {
      wx.navigateTo({
        url: '../../myCertification/myCertification',
      })
    } else {
      wx.navigateTo({
        url: '../../shopping/realName/realName',
      })
    }

  },
  //获取用户认证信息
  getRealnameInfo() {
    var that = this;
    app.ajax({
      url: "User/Realname/getAuthInfo",
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            authInfo: res.data.data
          })
        } else {
          that.setData({
            authInfo: { 'status_name': '待认证', 'status': '' }
          })
        }
      }
    })
  },
  //设备激活
  active: function (e) {
    var that = this;
    var order_id = e.currentTarget.dataset.orderid;
    wx.navigateTo({
      url: '../../activeDevice/activeDevice?order_id=' + order_id,
    })
  },

})